package com.ruoyi.common.mytools;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;

import java.util.Calendar;
import java.util.Map;


/**
 * JWT工具类
 */
public class jwtUtils {

    //private static final int expireTime=1000*60*60*24; //1天的时间
    private static final String secret="renrenKangAi"; //秘钥

    /**
     * jwt生成token
     * 自定义加载参数
     */
    public static String createToken(Map<String,String> userSetkeys) {
        JWTCreator.Builder builder=JWT.create();
        userSetkeys.forEach((k,v)->{
            builder.withClaim(k,v);
        });
        //设置时间
        Calendar instance=Calendar.getInstance();
        instance.add(Calendar.DATE,7);
        builder.withExpiresAt(instance.getTime()); //设置7天后过期
        return builder.sign(Algorithm.HMAC256(secret)).toString();  //进行签名并返回
    }

    /**
     * 验证token
    */
    public static void verify(String token){
        JWTVerifier jwtVerifier = JWT.require(Algorithm.HMAC256(secret)).build();
        DecodedJWT verify=jwtVerifier.verify(token);
    }

    /**
    * 获取token信息
    *  @return
    */
    public static DecodedJWT getToken(String token){
        return JWT.require(Algorithm.HMAC256(secret)).build().verify(token);
    }


 } /*Class End*/
